package com.heima.feigns.fallback;

import com.heima.feigns.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @作者 itcast
 * @创建日期 2021/9/20 14:48
 **/
@Component
@Slf4j
public class WemediaFeignFallback implements FallbackFactory<WemediaFeign> {
    @Override
    public WemediaFeign create(Throwable throwable) {
        // 调用指定微服务出现异常   throwable 异常对象
        throwable.printStackTrace();
        // 准备服务降级方法
        return new WemediaFeign() {
            // 服务降级方法
            @Override
            public ResponseResult<WmUser> save(WmUser wmUser) {
                log.info("请求参数: {}",wmUser);
                log.error("执行远程调用时:  WemediaFeignFallback save 出现异常: {}",throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }
            // 服务降级方法
            @Override
            public ResponseResult<WmUser> findByName(String name) {
                log.info("请求参数: {}",name);
                log.error("执行远程调用时:  WemediaFeignFallback findByName 出现异常: {}",throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult<WmNews> findWmNewsById(Integer id) {
                log.info("请求参数: {}",id);
                log.error("执行远程调用时:  findWmNewsById findByName 出现异常: {}",throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }

            @Override
            public ResponseResult updateWmNews(WmNews wmNews) {
                log.info("请求参数: {}",wmNews);
                log.error("执行远程调用时:  updateWmNews findByName 出现异常: {}",throwable.getMessage());
                return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
            }
        };
    }
}